$(function (){
    loadTeacherList()
});
function loadTeacherList(){
    $.ajax({
        url:"/api/teacher/list"
    }).done(function (data){
        //console.log(data)
        let html="";
        data.forEach((element,index) => {
            html+= "<tr>"
            html+= "<td>"+element.id+"</td>"
            html+= "<td>"+element.name+"</td>"
            html+= "<td>"+element.sex+"</td>"
            html+= "<td>"+element.age+"</td>"
            html+= "<td>"+element.tno+"</td>"
            html+= "<td> <a href='#' onclick='showTeacherDlg("+element.id+")'>编辑</a> | <a href='#' onclick='deleteTeacher("+element.id+")'>删除</a></td>";
            html+= "</tr>"
        })
        $("#teacherTb").html(html)
    });
}
let layerIndex;
function showTeacherDlg(id) {
    let title="新增老师"
    if(id){
        title="编辑老师"
        $("#formId").css("display","block");
        $.ajax({
            url:"/api/teacher/"+id,
            method:"GET"
        }).done(result=>{
            console.log(result)
            $.each(result,function(key, value){
                var field = $('#teacForm [name="'+ key +'"]');
                if(field.is(':radio')) {
                    field.filter('[value="'+ value +'"]').prop('checked',true);
                }else if (field.is(':checkbox')) {
                    field.prop('checked', value === "yes");
                }else {
                    field.val(value);
                }
            });
        })
    }else{
        $("#teacForm")[0].reset();
        $("#formId").css("display","none");
    }
    layerIndex=layer.open({
        type: 1,
        title: title,
        area: ['520px', 'auto'],
        content: $('#teacForm')
    });
}
layui.use(function (){
    layui.form.on('submit(teac-dlg)',function (data){
        event.preventDefault();
        commitTeaBlg();
    })
});
function  commitTeaBlg(){
    let id= $("#id").val()
    let formData = $("#teacForm").serialize();
    if (id!=null && id!="") {
        $.ajax({
            url: "/api/teacher/update",
            method: "PUT",
            data: formData
        }).done(result=>{
            console.log(result);
            if(result.id){
                loadStudentList();
                console.log("add success")
                if(layerIndex)
                    layer.close(layerIndex)
            }
        }).fail((jqXHR,textStatus,errorThrown)=>{
            console.error("Request failed: "+textStatus + " - " + errorThrown);
            alert("An error occurred. Please try again.");
        });
    }
    $.ajax({
        url: "/api/teacher/add",
        method: "POST",
        data: formData
    }).done(result=>{
        console.log(result);
        if(result.id){
            loadTeacherList();
            console.log("add success")
            if(layerIndex)
                layer.close(layerIndex)
        }
    }).fail((jqXHR,textStatus,errorThrown)=>{
        console.error("Request failed: "+textStatus + " - " + errorThrown);
        alert("An error occurred. Please try again.");
    });
    $("#btnOK").prop("disabled",true).addClass("layui-btn-disabled");
}
function deleteTeacher(id) {
    $.ajax({
        url: "/api/teacher/delete/" + id,
        method: "DELETE",
    }).done(function (result) {
        console.log(result);
        loadTeacherList();
        alert("删除成功！");
    }).fail(function (jqXHR, textStatus, errorThrown) {
        console.error("Request failed: " + textStatus + " - " + errorThrown);
        alert("删除失败，请稍后再试。");
    });
}